package com.eystar.config;

import com.eystar.ext.aop.ExceptionInterceptor;
import com.eystar.ext.aop.PageInterceptor;
import com.eystar.ext.aop.PathInterceptor;
import com.eystar.ext.engine.MyLongExt;
import com.eystar.ext.plugin.crate.CratePlugin;
import com.eystar.ext.plugin.tablebind.AutoTableBindPlugin;
import com.eystar.ext.plugin.tablebind.ModelExt;
import com.eystar.ext.route.AutoBindRoutes;
import com.eystar.pdc.aop.auth.AuthInterceptor;
import com.eystar.pdc.aop.log.LogInterceptor;
import com.eystar.pdc.aop.log.ThreadLog;
import com.eystar.pdc.controller.BaseController;
import com.eystar.pdc.directive.AuthHasDirective;
import com.eystar.pdc.directive.AuthJsonDirective;
import com.eystar.pdc.directive.DicForDirective;
import com.eystar.pdc.directive.DicItemGetDirective;
import com.eystar.pdc.directive.DicJsonDirective;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;

public class BaseConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("little_config.txt");
		me.setDevMode(true);
		me.setError500View("/common/500.html");
		me.setError500View("/common/404.html");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void configRoute(Routes me) {
		AutoBindRoutes abr = new AutoBindRoutes();
		abr.setBaseViewPath("/WEB-INF/templates");
		abr.addExcludeClasses(BaseController.class);
		me.add(abr);
	}

	@Override
	public void configEngine(Engine me) {
		// 添加指令
		me.addDirective("dic_for", DicForDirective.class);
		me.addDirective("dic_json", DicJsonDirective.class);
		me.addDirective("dic_item_get", DicItemGetDirective.class);
		me.addDirective("auth_json", AuthJsonDirective.class);
		me.addDirective("auth_has", AuthHasDirective.class);
		me.setDevMode(true);
		Engine.addExtensionMethod(Long.class, MyLongExt.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void configPlugin(Plugins me) {
		// MySQL数据库
		DruidPlugin dsMysql = new DruidPlugin(
				PropKit.get("mysql.jdbc"),PropKit.get("mysql.user"), 
				PropKit.get("mysql.password"),PropKit.get("mysql.driver"));
		AutoTableBindPlugin arpMysql = new AutoTableBindPlugin("mysql",dsMysql);
		arpMysql.addExcludeClasses(ModelExt.class);
		arpMysql.setDevMode(true);
		arpMysql.setShowSql(true);
		me.add(dsMysql);
		me.add(arpMysql);
		
		// Crate数据库
		DruidPlugin dsCrate = new DruidPlugin(
				PropKit.get("crate.jdbc"),PropKit.get("crate.user"), 
				PropKit.get("crate.password"),PropKit.get("crate.driver"));
		CratePlugin arpCrate = new CratePlugin("crate",dsCrate);
		arpCrate.setDialect(new PostgreSqlDialect());
		arpCrate.setDevMode(true);
		arpCrate.setShowSql(true);
		me.add(dsCrate);
		me.add(arpCrate);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		me.add(new PathInterceptor());
		me.add(new ExceptionInterceptor("/common/500.html"));
		me.add(new PageInterceptor());
		me.add(new AuthInterceptor());
		me.add(new LogInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		
	}

	public void afterJFinalStart(){
		// 开启日志启动线程
		ThreadLog.startSaveDBThread();
	}
}
